//
//  MAJSExecutorProtocol.h
//  MiniApp
//
//  Created by zilinzhou on 2018/11/22.
//  Copyright © 2018 tencent. All rights reserved.
//

#import <Foundation/Foundation.h>
@class MABaseApplication;

NS_ASSUME_NONNULL_BEGIN

typedef void(^MAPageWebViewCallback)(id _Nullable result, NSError * _Nullable error);

@protocol MAJSExecutorProtocol <NSObject>

@property (nonatomic, weak) MABaseApplication* application;

- (void)evaluateJS:(NSString *)script callback:(MAPageWebViewCallback _Nullable)callback;

- (void)doCallback:(NSInteger)callbackID
            result:(NSString *)result
 completionHandler:(MAPageWebViewCallback _Nullable)completionHandler;

- (void)doSubscribe:(NSString *)event
               data:(NSString *)data
          webviewId:(NSInteger)webviewId
  completionHandler:(MAPageWebViewCallback _Nullable)completionHandler;

@optional
- (void)onReceiveWorkerMsg:(NSString *)data workerId:(NSInteger)workerId;

/// 同步调用subscribeHandler接口，并返回执行结果
- (id)doSyncSubscribe:(NSString *)event
                 data:(NSString *)data
            webviewId:(NSInteger)webviewId;

/**
 小游戏使用
 小游戏实现中 doSubscribe:data:webviewId:completionHandler 放到帧尾执行
 immediately:YES 立即执行  NO:帧尾执行
 */
- (void)doSubscribe:(NSString *)event
               data:(NSString *)data
          webviewId:(NSInteger)webviewId
          immediately:(BOOL)immediately
  completionHandler:(MAPageWebViewCallback _Nullable)completionHandler;

/**
 *  // 获取executor的执行队列，释放网络请求上下文时会调用
 *  // 小程序是miniapp_queue，小游戏是主线程
 *  // 临时方案，最终方案应该去掉这个接口，释放上下文时需要加锁
 */
- (dispatch_queue_t)getCurrentQueue;

- (void)terminate;

- (NSThread *)executeThread;

@end

NS_ASSUME_NONNULL_END
